package tech.aistar.day06.extend;

/**
 * 本类用来演示:
 *
 * @author: success
 * @date: 2021/3/15 2:25 下午
 */
public class TestAnimalArray {
    public static void main(String[] args) {
        //只有定义一个数组
        Animal[] animals = new Animal[2];

        //dog
        Animal dog = new Dog("admin",100.0d);

        //cat
        Animal cat = new Cat("tom",100.0d);

        animals[0] = dog;
        animals[1] = cat;

        //何时才能父类强制类型转换成子类
        //一定要满足a instanceof b
        System.out.println(dog instanceof Dog);//true
        System.out.println(dog instanceof Animal);//true

        System.out.println(dog instanceof Cat);//false

        for (Animal animal : animals) {
            //访问共性的方法
           /* animal.sleep();
            System.out.println("-----");*/

           //搞乱
            //调用各自子类中特有的方法

            //强制类型转换之前,需要先进行类型的判断
            if(animal instanceof Dog) {
                Dog d = (Dog) animal;
                d.watchDoor();
            }

            if(animal instanceof Cat) {
                Cat c = (Cat) animal;
                c.catchMouse();
            }
        }
    }
}
